<?php
class Index_Action extends Action{
	public function init(){
		$this->redis = new Redis();
		$this->redis->connect('127.0.0.1', 6379);
		$this->redis->select($this->_config['redisdb']);
	}
	public function index(){
		if($_GET['admin'] != '1989'){exit;}
		$result = array(

			'thread_count' => $this->redis->get('thread_count'),
			'vaild_thread_count' => $this->redis->get('vaild_thread_count'),
			'proxy_vaild_time' => $this->redis->get('proxy_vaild_time'),
			'proxy_thread' =>$this->redis->get('proxy_thread'),
			'vaild_proxy_thread' =>$this->redis->get('vaild_proxy_thread'),
			'notvaild_proxy_thread' =>$this->redis->get('notvaild_proxy_thread'),
			'proxy_thread_' => $this->redis->get('proxy_thread_'),
			'job_thread' => $this->redis->get('job_thread'),
			'job_thread_' => $this->redis->get('job_thread_'),

			'proxy_spider_thread_count' => $this->redis->get('proxy_spider_thread_count'),
			'proxy_spider_thread_wait_time' => $this->redis->get('proxy_spider_thread_wait_time'),
			'spider_thread' => $this->redis->get('spider_thread'),

			'proxy_vaild'=> count($this->redis->hkeys('proxy_vaild')),
			'proxy_notvaild' => count($this->redis->smembers('proxy_notvaild'))

		 );


		$this->_view->assign('thread',$result);
		$this->_view->display('index/index.htm');
	}
	public function test(){
		print_r($this->redis->smembers('proxy_notvaild'));
	}
	public function set(){
		if($_POST){
			header('Content-type: application/json');
			switch($_POST['do']){
				case 'thread_count':
					if(!is_numeric($_POST['param'])){ echo json_encode(array('status'=>'error','param'=>'必须为整数型'));exit;}
					$this->redis->set('proxy_thread',0);
					$this->redis->set('thread_count',intval($_POST['param']));
					$this->redis->set('proxy_thread',1);
					echo json_encode(array('param'=>$_POST['param'],'status'=>'success'));
				break;
				case 'vaild_thread_count':
					if(!is_numeric($_POST['param'])){ echo json_encode(array('status'=>'error','param'=>'必须为整数型'));exit;}
					$this->redis->set('proxy_thread_',0);
					$this->redis->set('vaild_thread_count',intval($_POST['param']));
					$this->redis->set('proxy_thread_',1);
					echo json_encode(array('param'=>$_POST['param'],'status'=>'success'));
				break;
				case 'proxy_vaild_time':
					//注释部份开启可能造成验证时间双倍
					if(!is_numeric($_POST['param'])){ echo json_encode(array('status'=>'error','param'=>'必须为整数型'));exit;}
					//$this->redis->set('job_thread_',0);
					$this->redis->set('proxy_vaild_time',intval($_POST['param']));
					//$this->redis->set('job_thread_',1);
					echo json_encode(array('param'=>$_POST['param'],'status'=>'success'));
				break;
				case 'proxy_thread':
					if($_POST['param'] =='开启'){
						$this->redis->set('proxy_thread',0);
						echo json_encode(array('param'=>'关闭','param1'=>'开启','status'=>'success'));
					}elseif($_POST['param'] =='关闭'){
						$this->redis->set('proxy_thread',1);
						echo json_encode(array('param'=>'开启','param1'=>'关闭','status'=>'success'));
					}
				break;
				case 'vaild_proxy_thread':
					if($_POST['param'] =='开启'){
						$this->redis->set('vaild_proxy_thread',0);
						echo json_encode(array('param'=>'关闭','param1'=>'开启','status'=>'success'));
					}elseif($_POST['param'] =='关闭'){
						$this->redis->set('vaild_proxy_thread',1);
						echo json_encode(array('param'=>'开启','param1'=>'关闭','status'=>'success'));
					}
				break;
				case 'notvaild_proxy_thread':
					if($_POST['param'] =='开启'){
						$this->redis->set('notvaild_proxy_thread',0);
						echo json_encode(array('param'=>'关闭','param1'=>'开启','status'=>'success'));
					}elseif($_POST['param'] =='关闭'){
						$this->redis->set('notvaild_proxy_thread',1);
						echo json_encode(array('param'=>'开启','param1'=>'关闭','status'=>'success'));
					}
				break;
				case 'proxy_thread_':
					if($_POST['param'] =='开启'){
						$this->redis->set('proxy_thread_',0);
						echo json_encode(array('param'=>'关闭','param1'=>'开启','status'=>'success'));
					}elseif($_POST['param'] =='关闭'){
						$this->redis->set('proxy_thread_',1);
						echo json_encode(array('param'=>'开启','param1'=>'关闭','status'=>'success'));
					}
				break;
				case 'job_thread':
					if($_POST['param'] =='开启'){
						$this->redis->set('job_thread',0);
						echo json_encode(array('param'=>'关闭','param1'=>'开启','status'=>'success'));
					}elseif($_POST['param'] =='关闭'){
						$this->redis->set('job_thread',1);
						echo json_encode(array('param'=>'开启','param1'=>'关闭','status'=>'success'));
					}
				break;
				case 'job_thread_':
					if($_POST['param'] =='开启'){
						$this->redis->set('job_thread_',0);
						echo json_encode(array('param'=>'关闭','param1'=>'开启','status'=>'success'));
					}elseif($_POST['param'] =='关闭'){
						$this->redis->set('job_thread_',1);
						echo json_encode(array('param'=>'开启','param1'=>'关闭','status'=>'success'));
					}
				break;

				case 'proxy_spider_thread_count':
					if(!is_numeric($_POST['param'])){ echo json_encode(array('status'=>'error','param'=>'必须为整数型'));exit;}
					$this->redis->set('spider_thread',0);
					$this->redis->set('proxy_spider_thread_count',intval($_POST['param']));
					sleep(3); //用来改变设置
					$this->redis->set('spider_thread',1);
					echo json_encode(array('param'=>$_POST['param'],'status'=>'success'));
				break;

				case 'proxy_spider_thread_wait_time':
					if(!is_numeric($_POST['param'])){ echo json_encode(array('status'=>'error','param'=>'必须为整数型'));exit;}
					$this->redis->set('proxy_spider_thread_wait_time',intval($_POST['param']));
					echo json_encode(array('param'=>$_POST['param'],'status'=>'success'));
				break;

			}
		}
	}

}